package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.TokenService;
import cn.gjculture.shopping.api.service.VipOrderService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/vipOrder")
public class VipOrderController {


    @Autowired
    VipOrderService vipOrderService;

    @Autowired
    TokenService tokenService;


    @ApiOperation(value = "提交订单", notes = "提交订单")
    @ApiImplicitParam(name = "order", value = "订单对象", required = true, dataType = "Object")
    @PostMapping
    public Return<InterfaceParam> save(String order, ParamData paramData, String sign) {
        Return ret=new Return();
        Order order1 = JSONObject.parseObject(order, new TypeReference<Order>() {});
        if (!tokenService.verifySign(order,order1,paramData,sign)) {
            ret.getCommon().setMessage("签名验证失败");
            return ret;
        }
        if (vipOrderService.save(order1, ret) > 0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }


    @RequestMapping(value = "/toPay")
    public String toPay(@RequestHeader(value="User-Agent") String userAgent, Model model, UnifyParam unifyParam){
        log.info("User-Agent:{}",userAgent);
        String payStr = null;
        if(userAgent.contains("MicroMessenger")){
            model.addAttribute("payChannels","weChatPay");
            unifyParam.setPayChannel("weChatPay");
            payStr=vipOrderService.unifyOrder(unifyParam);
        }else if(userAgent.contains("Alipay")){
            //model.addAttribute("payChannels","aliPay");
            unifyParam.setPayChannel("aliPay");
            payStr=vipOrderService.unifyOrder(unifyParam);
            if(!StringUtils.isEmpty(payStr)){
                return "redirect:"+payStr;
            }
        }
        model.addAttribute("payStr",payStr);
        return "toPay";
    }

    @PostMapping(value = "/refund")
    public Return refund(Order order, Return ret) {
        vipOrderService.refund(order, ret);
        return ret;
    }
    @ApiOperation(value = "查询退款订单", notes = "查询退款订单")
    @GetMapping(value = "/refundFlow/{orderId}")
    public Return<RefundFlow> queryRefundFlow(@PathVariable(value = "orderId")String orderId){
        Return ret=new Return();
        RefundFlow refundFlow=vipOrderService.queryRefundFlow(orderId);
        if(refundFlow!=null){
            ret.setData(refundFlow);
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @PutMapping(value = "/refundFlow")
    public Return handleRefund(RefundFlow refundFlow){
        Return ret=new Return();
        if(vipOrderService.handleRefund(refundFlow,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }


    @GetMapping
    public Return<List<Order>> query(OrderParams params, PageInfo pageInfo, Return ret){
        List<Order> orders= vipOrderService.query(params);
        ret.setData(orders);
        ret.getCommon().setRet(true);
        return ret;
    }

    @PutMapping(value = "/confirmGoods")
    public Return updateStatus(String orderId){
        Return ret=new Return();
        Order order=new Order();
        order.setOrderId(orderId);
        order.setStatus(10);
        if(vipOrderService.updateStatus(order)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping(value = "/export")
    public void export(OrderParams orderParams, HttpServletResponse response) throws IOException {
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("VIP订单.xlsx", "utf-8"));
        vipOrderService.export(orderParams,response);
    }
}
